/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.algorithm.answers;

import com.hyts.algorithm.Solution;
import lombok.Getter;
import lombok.Setter;
import org.junit.Test;

import java.util.LinkedList;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.algorithm.answers
 * @author:LiBo/Alex
 * @create-date:2019-11-01 09:22
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class day01 implements Solution {


    /**
     * 从数组中找到唯一的一组两个数值，返回下标 条件为
     * 加和等于N，如果不存在 则返回{-1,-1}
     * 且不能重复使用使用过得数字
     */
    @Test
    @Override
    public void solution1() {

        int [] a = {7,2,7,11,15};

        int sum = 9;

        boolean flag = true;

        //int[] used = new int[];

        for(int i=0;i<a.length&&flag;i++){
            for(int j = i+1;j<a.length&&flag ;j++){
                if( a[i]+a[j]  == 9){
                    flag = false;
                    System.out.println(i+","+j);
                }
            }
        }
    }

    /**
     * 定义两组链表结构，求每项的相加的和 之后逆序输出
     * (1,5,7) -> (3,5,4) = (4,0,2) -> (2,0,4)
     */
    @Test
    @Override
    public void solution2() {

        class Node {

            @Setter
            @Getter
            int value;

            @Setter
            @Getter
            Node next;

            public Node(int value, Node next) {
                this.value = value;
                this.next = next;
            }
        }

        Node nodeList1 = new Node(1,new Node(5,new Node(7,null)));
        Node nodeList2 = new Node(3,new Node(5,new Node(4,null)));
        LinkedList linkedList = new LinkedList();
        boolean comingSite;
        Node currentNode1 = null;
        Node currentNode2 = null;
        comingSite = false;
        int KEY_THRESHOLD = 9;
        do{
            if(currentNode1 == null && currentNode2 == null){
                currentNode1 = nodeList1;
                currentNode2 = nodeList2;
            }
            int value = currentNode1.value+currentNode2.value+(comingSite?1:0);
            if(value > KEY_THRESHOLD){
                comingSite = true;
                value = value % 10;
            }else{
                comingSite = false;
            }
            linkedList.addFirst(value);
            currentNode1 = currentNode1.next;
            currentNode2 = currentNode2.next;
        }while (currentNode1!=null && currentNode2!=null);
        System.out.println(linkedList);
    }

    /**
     * 定义一个字符串，查找其中没有任何重复元素的最长子串
     * abcabcbb abc
     * bbbbbb b
     * pwwkew wke
     */
    @Test
    @Override
    public void solution3() {

        String str = "pwwkew";

        char[] ordinayStr = str.toCharArray();

        StringBuilder stringBuilder = new StringBuilder();

        String currentMaxStr = "";

        for(int i = 0 ; i< ordinayStr.length ;i++){
            if(stringBuilder.indexOf(String.valueOf(ordinayStr[i])) > -1){
                if(currentMaxStr.length() < stringBuilder.length()){
                    currentMaxStr = stringBuilder.toString();
                }
                stringBuilder = new StringBuilder(String.valueOf(ordinayStr[i]));
            }else{
                stringBuilder.append(ordinayStr[i]);
            }
        }
        if(stringBuilder.length() > currentMaxStr.length()){
            currentMaxStr = stringBuilder.toString();
        }
        System.out.println(currentMaxStr);
    }
}
